package com.xiaotu.spider.license.pipeline;

import java.util.Date;
import java.util.Set;

import org.springframework.stereotype.Component;

import com.xiaotu.common.model.TVChangeModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DataRedisKeyUtils;
import com.xiaotu.common.util.DateUtils;
import com.xiaotu.spider.APipeline;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

/**
 * @类名 TVChangePipeline
 * @日期 2017年10月09日
 * @作者 王艳龙
 * @功能 电视剧备案变更pipeline
 */
@Component("TVChangePipeline")
public class TVChangePipeline extends APipeline
{
    @Override
    protected void doProcess(ResultItems resultItems, Task task)
    {
    	Set<TVChangeModel> set = resultItems.get(TVChangeModel.class.getName());
        if (set == null || set.isEmpty())
            return;
        String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.TV_Change_YYYYMMDD);
        for (TVChangeModel tvChangeModel : set) {
        	try {
				Thread.sleep(1000);
				cacheHandler.hSet(key,System.currentTimeMillis()+"",tvChangeModel);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
        	cacheHandler.setCacheTimeout(key);
        	
        	
        	Set<String> urlSet = resultItems.get(AUrlPageProcessor.URL_KEY);
        	if (urlSet == null || urlSet.isEmpty())
                return;
        	for (String url : urlSet) {
        		if (cacheHandler.hExists(Constants.DataRedisKey.Change_History.toString(), url))
        			return;
        		cacheHandler.hSet(Constants.DataRedisKey.Change_History.toString(), url, DateUtils
        				.fomartDateToString(new Date(), DateUtils.SECONDS_FORMAT));
			}
    }

}
